<!-- 委托申请 -->
<template>
  <div>
    <div class="searchbox">
      <el-form :inline="true" size="small" label-width="100px">
        <jt-easy-collapse :has-expand="true">

<template slot="expand">
        <el-form-item label="任务单号">
                                          <el-input size="small" v-model="form.taskNo" placeholder="任务单号"></el-input>
                                        </el-form-item>
                                        <el-form-item label="零件名称">
                                          <el-input size="small" v-model="form.partName" placeholder="零件名称"></el-input>
                                        </el-form-item>
                                        <el-form-item label="零件号">
                                          <el-input size="small" v-model="form.partNumber" placeholder="零件号"></el-input>
                                        </el-form-item>
                                        <el-form-item label="热处理炉号">
                                          <el-input size="small" placeholder="热处理炉号" v-model='form.heatingNumber'></el-input>
                                        </el-form-item>
                                        <el-form-item label="零件图号">
                                          <el-input size="small" v-model="form.partGraphNumber" placeholder="零件图号"></el-input>
                                        </el-form-item>
                                        <el-form-item label="样品阶段">
                                          <el-select v-model="form.sampleStatusPhase" filterable placeholder="请选择" style="width: 200px;">
                                         <el-option  :label="all" :value="''"></el-option>
                                         <el-option v-for="item in sampleStatusPhaseList" :key="item.code" :label="item.name" :value="item.code">
                                          </el-option>
                                        </el-select>
                                        </el-form-item>
                                        <el-form-item label="项目号">
                                          <el-input size="small" v-model="form.projectNo" placeholder="项目号"></el-input>
                                        </el-form-item>
                                        <el-form-item label="报验批次信息">
                                          <el-input size="small" v-model="form.batchNumber" placeholder="报验批次信息"></el-input>
                                        </el-form-item>
                                        <el-form-item label="材料牌号">
                                          <el-input size="small" v-model="form.materialNo" placeholder="材料牌号"></el-input>
                                        </el-form-item>
                                        <el-form-item style="margin-left: 20px;">
                                          <el-button size="mini" type="primary" @click='showList'>查询</el-button>
                                        </el-form-item>
                                        <el-form-item>
                                          <el-button size="mini" @click='clearParam'>清除</el-button>
                                        </el-form-item>
                                        </template>
        </jt-easy-collapse>
      </el-form>
    </div>
    <div class="contentbox">
      <div style="float: left;padding-left: 20px;font-size: 15px;color: #666;padding-top: 6px;">
        委托单信息 共 <span style="color: red;">{{pageTotal}}</span> 条数据
      </div>
      <el-row style="float: right;margin-right: 20px;margin-bottom: 15px;" type="flex" justify="end">
        <el-col>
          <jt-table-column-filter tableName='applicationForEntrustment' :table-key.sync="tableKey" :header-data.sync="tableHeaderArr" :filter-column.sync="filterColumnData"></jt-table-column-filter>
          <el-button size="small" type="primary" @click="addOrder()">新建</el-button>
          <el-button size="small" type="primary" @click="showHistory()">从历史委托单新建</el-button>
          <el-button size="small" type="primary" @click="submitOrder">提交</el-button>

          <!-- <el-button size="small" type="primary">提交并打印</el-button> -->
        </el-col>
      </el-row>
      <!-- 新增 -->
      <add-entrust-order :show.sync="isShowOrder" :copyId.sync='copyId' @refreshAdd="showList"></add-entrust-order>
      <!-- 修改 -->
      <modify-entrust-order :show.sync="isShowModify" :taskId="taskId" @refreshEdit="showList"></modify-entrust-order>
      <!-- 详情 -->
      <order-entrust-detail :show.sync="isShowEntrustDetail" :detailId="detailId" :detailName="detailName"></order-entrust-detail>
      <!-- 表格 -->
      <el-table :data="tableData" :key='tableKey' v-loading="loading" @selection-change="handleSelectionChange" stripe>
        <el-table-column type="selection" width="50" align="center" :resizable="false"></el-table-column>
        <el-table-column type="index" label="序号" width="50" align="center" :resizable="false"></el-table-column>
        <el-table-column align="center" :key='item' v-for='item in filterColumnData' :resizable="false"
            :label="getTableHeaderLabel(item)"
            :prop="item"
            :min-width="getTableColumnWidth(item)"
            :fixed="getTableColumnFixed(item)"
            :sortable="getTableColumnSort(item)" >
<template slot-scope="scope" width='200'>
   {{scope.row[item]}}
  <el-button v-if="item === 'operate'" size="small" type="text" @click="modifyOrder(scope.row)">修改</el-button>
  <el-button v-if="item === 'operate'" size="small" type="text" @click="detailOrder(scope.row)">详情</el-button>
  <el-button v-if="item === 'operate'" size="small" type="text" @click="copyOrder(scope.row)">拷贝</el-button>
  <el-button v-if="item === 'operate'" size="small" type="text" @click="deleteOrder(scope.row)">删除</el-button>
</template>
        </el-table-column>
      </el-table>
      <el-row type="flex" justify="center">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="pageSizeArr"
          :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="pageTotal"></el-pagination>
      </el-row>
    </div>

    <el-dialog title="历史委托单" :visible.sync="historyVisable" :fullscreen="isFullScreen" :custom-class="isFullScreen?'fullscreen-default-dialog':'vertical-center-dialog'" append-to-body>
       <button type="button" aria-label="fullScreen" class="el-dialog__headerbtn" style="margin-right:25px;" @click="isFullScreen = !isFullScreen">
                                          <i class="el-dialog__close iconfont icon-fuzhicopy22"></i>
                                      </button>
      <div class="searchbox">
          <el-form :inline="true" size="small" label-width="100px">
              <el-form-item label="任务单号">
                                          <el-input size="small" v-model="historyForm.taskNo" placeholder="任务单号"></el-input>
                                        </el-form-item>
                                        <el-form-item label="零件名称">
                                          <el-input size="small" v-model="historyForm.partName" placeholder="零件名称"></el-input>
                                        </el-form-item>
                                        <el-form-item label="零件号">
                                          <el-input size="small" v-model="historyForm.partNumber" placeholder="零件号"></el-input>
                                        </el-form-item>
                                        <el-form-item style="margin-left: 20px;">
                                          <el-button size="mini" type="primary" @click='showHistoryList'>查询</el-button>
                                        </el-form-item>

            </el-form>
        </div>
      <el-table :data="tableHistoryData">
        <el-table-column :index="indexMethod" type='index' label='序号' width="50" align="center" :resizable="false"></el-table-column>
        <el-table-column property="taskNo" label="委托单号" width="200"></el-table-column>
        <el-table-column property="applyTimeStr" label="委托时间" width="200"></el-table-column>
        <el-table-column property="heatingNumber" label="热处理炉号" width="200"></el-table-column>
        <el-table-column property="partNumber" label="零件号" width="200"></el-table-column>
        <el-table-column property="partName" label="零件名称" width="200"></el-table-column>

        <el-table-column align="center" lable='操作' width='150'>
    <template slot-scope="scope" width='200'>
      <el-button size="small" type="text" @click="copyOrder(scope.row)">拷贝</el-button>
    </template>
        </el-table-column>
      </el-table>
      <el-row type="flex" justify="center">
        <el-pagination @size-change="handleHistorySizeChange" @current-change="handleHistoryCurrentChange" :current-page="currentHistoryPage" :page-sizes="pageSizeArr"
          :page-size="pageHistorySize" layout="total, sizes, prev, pager, next, jumper" :total="pageHistoryTotal"></el-pagination>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
  import JtTableColumnFilter from 'components/jt-table-column-filter/index'; // 导入表格列筛选组件
  import JtEasyCollapse from 'components/jt-easy-collapse/index';
  import AddEntrustOrder from 'dialog/experimental-management/application-for-entrustment/add-test-entrust-order';
  import ModifyEntrustOrder from 'dialog/experimental-management/application-for-entrustment/modify-test-entrust-order';
  import EntrustDetail from 'dialog/experimental-management/application-for-entrustment/order-entrust-detail';
  import {
    getApplyListService,
    postSubmitApplyService,
    postDeleteApplyService,
    getSampleStatusPhase,getTaskManageListService
  } from 'service/task';

  import {
    APPLY_TYPE_LIST,
    APPLY_STATUS_LIST
  } from '../../../service/constant'

  import {
    getLableFromList,
    dateFormat
  } from '../../../utils/common'

  export default {
    name: 'ApplicationForEntrustment',
    components: {
      'jt-table-column-filter': JtTableColumnFilter,
      'jt-easy-collapse': JtEasyCollapse,
      'add-entrust-order': AddEntrustOrder,
      'modify-entrust-order': ModifyEntrustOrder,
      'order-entrust-detail': EntrustDetail,
    },
    data() {
      return {
        isFullScreen: true,
        tableKey: 1, // table key
        filterColumnData: [],
        historyVisable:false,
        copyId:'',
        count: 0,
        selectRow: [],
        historyForm:{taskType:1},
        all:'全部',
        tableHeaderArr: [{
            'name': 'taskNo',
            'alias': '任务单号',
            'default': true,
            'width': 270,
            'fixed': false,
            'sort': true
          },
          {
            'name': 'applyTypeStr',
            'alias': '任务类型',
            'default': true,
            'width': 170,
            'fixed': false,
            'sort': false
          },
          {
            'name': 'applyUserName',
            'alias': '委托人',
            'default': false,
            'width': 170,
            'fixed': false,
            'sort': false
          },
          {
            'name': 'applyTimeStr',
            'alias': '委托日期',
            'default': true,
            'width': 170,
            'fixed': false,
            'sort': true
          },
          {
            'name': 'taskStatusStr',
            'alias': '任务单状态',
            'default': true,
            'width': 230,
            'fixed': false,
            'sort': false
          },
          {
            'name': 'heatingNumber',
            'alias': '热处理炉号',
            'default': true,
            'width': 230,
            'fixed': false,
            'sort': true
          },
          {
            'name': 'quantity',
            'alias': '样品数量',
            'default': true,
            'width': 230,
            'fixed': false,
            'sort': true
          },
          {
            'name': 'partNumber',
            'alias': '零件号',
            'default': true,
            'width': 230,
            'fixed': false,
            'sort': true
          },
          {
            'name': 'partName',
            'alias': '零件名称',
            'default': true,
            'width': 230,
            'fixed': false,
            'sort': true
          },
          {
            'name': 'projectNo',
            'alias': '项目号',
            'default': true,
            'width': 230,
            'fixed': false,
            'sort': true
          },
          {
            'name': 'sampleStatusPhaseStr',
            'alias': '样品阶段',
            'default': true,
            'width': 230,
            'fixed': false,
            'sort': true
          },
          {
            'name': 'materialNo',
            'alias': '材料牌号',
            'default': true,
            'width': 230,
            'fixed': false,
            'sort': true
          },
          {
            'name': 'batchNumber',
            'alias': '报验批次信息',
            'default': true,
            'width': 230,
            'fixed': false,
            'sort': true
          },
          {
            'name': 'operate',
            'alias': '操作',
            'default': true,
            'width': 190,
            'fixed': 'right',
            'sort': false
          }
        ],
        tableData: [],
        tableHistoryData: [],
        form: {
          taskNo: '',
          partName: '',
          partNumber: '',
          partGraphNumber: '',
          sampleStatusPhase: null,
          projectNo: '',
          batchNumber: '',
          materialNo: '',
          taskType:1
        },
        currentPage: 1,
        pageNumber: 1,
        currentHistoryPage: 1,
        pageHistoryNumber: 1,
        sampleStatusPhaseList: [],
        pageSizeArr: [10, 20, 50, 100],
        pageSize: 10,
        pageTotal: 10,
        pageHistorySize: 10,
        pageHistoryTotal: 10,
        loading: false,
        selectedVal: '',
        sort: '',
        order: '',
        isShowOrder: false,
        isShowModify: false,
        isShowEntrustDetail: false,
        taskId: "", // 修改 任务单id
        detailId: "", // 详情 任务单id
        detailName:"",
      }
    },
    watch: {
      isShowOrder() {
        if (!this.isShowOrder) {
          this.showList();
        }
      }
    },
    mounted() {
      getSampleStatusPhase().then(res => {
        this.sampleStatusPhaseList = res.data.result;
      })
      this.showList();
      this.showHistoryList();
    },
    methods: {
       indexMethod(index) {
        return index + this.pageSize * (this.currentPage-1) + 1;
      },
      showHistory(){
        this.historyVisable=true;
      },
      clearParam() {
        this.form = {};
        this.showList();
      },
      // 新增
      addOrder() {
        this.isShowOrder = true;
        this.copyId = "";
      },
      copyOrder(row) {
        this.copyId = row.id;
        this.isShowOrder = true;
        this.historyVisable=false;
      },
      // 详情
      detailOrder(row) {
        this.detailId = row.id;
        this.detailName = "理化检测任务单详情";
        this.isShowEntrustDetail = true;
      },
      // 修改
      modifyOrder(row) {
        this.taskId = row.id;
        this.isShowModify = true;
      },
       // 提交
      submitOrder() {
        if (this.selectRow.length <= 0) {
          this.$alert('请选择一条数据！', '提示', {
            type: 'warning'
          })
        } else {
          for(let i = 0; i < this.selectRow.length; i++) {
             postSubmitApplyService(this.selectRow[i].id)
            .then(res => {
              if (res.data.success) {
                this.count++;
                if(this.count == this.selectRow.length) {
                    this.$message({
            message: `提交成功`,
            type: 'success',
            duration: 1000
            });
                }
                this.showList();
              } else {
                this.$alert(res.data.message, '提示', {
                  type: 'error'
                })
              }
            })
          }
        }
      },
      // 删除
      deleteOrder(row) {
        // console.log(row)
        this.$confirm('是否删除此条数据?', '提示', {
          // confirmButtonText: '确定',
          // cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          postDeleteApplyService(row.id)
            .then(res => {
              if (res.data.success) {
                this.$message({
            message: res.data.message,
            type: 'success',
            duration: 1000
            });
                this.showList();
              } else {
                this.$alert(res.data.message, '提示', {
                  type: 'error'
                })
              }
            })
        }).catch(() => {});
      },
      // 获取表格多选的值
      handleSelectionChange(val) {
        // if (val.length > 1) {
        //   this.$alert('只能选择一条数据！', '提示', {
        //     type: 'warning'
        //   })
        // } else if (val.length == 1) {
        //   this.selectedVal = val[0].id;
        // }
        this.selectRow = val;
      },
      handleSizeChange(val) {
        this.pageSize = val;
        this.showList();
      },
      handleCurrentChange(val) {
        // this.partNumber = val;
        this.pageNumber = val;
        this.currentPage = val
        this.showList();
      },
      handleHistorySizeChange(val) {
        this.pageHistorySize = val;
        this.showHistoryList();
      },
      handleHistoryCurrentChange(val) {
        this.partHistoryNumber = val;
        this.currentHistoryPage = val;
        this.showHistoryList();
      },
      // 表格数据展示
      showList() {
        this.loading = true;
        getApplyListService({ ...this.form,
            pageNumber: this.pageNumber,
            pageSize: this.pageSize,
            sort: this.sort,
            order: this.order
          })
          .then(res => {
            // console.log(res)
            this.tableData = res.data.result.content;

            if (this.tableData) {
              for (let i = 0; i < this.tableData.length; i++) {
                this.tableData[i].applyTypeStr = getLableFromList(APPLY_TYPE_LIST, this.tableData[i].taskType);
                this.tableData[i].taskStatusStr = getLableFromList(APPLY_STATUS_LIST, this.tableData[i].taskStatus);
                this.tableData[i].applyUserName = this.tableData[i].applyUser ? this.tableData[i].applyUser.name : '';
                this.tableData[i].applyTimeStr = dateFormat(new Date(this.tableData[i].applyTime));
                this.tableData[i].sampleStatusPhaseStr = getLableFromList(this.sampleStatusPhaseList, this.tableData[i].sampleStatusPhase);
              }
            }

            console.log(this.tableData);

            // this.currentPage = res.data.result.pageable.pageNumber;
            this.pageSize = res.data.result.pageable.pageSize;
            this.pageTotal = res.data.result.totalElements;
            this.loading = false;
          });
      },
      showHistoryList() {
        getTaskManageListService({
            ...this.historyForm,
            pageNumber: this.partHistoryNumber,
            pageSize: this.pageHistorySize,
            sort: this.sort,
            order: this.order
          })
          .then(res => {
            // console.log(res)
            this.tableHistoryData = res.data.result.content;

            if (this.tableHistoryData) {
              for (let i = 0; i < this.tableHistoryData.length; i++) {
                this.tableHistoryData[i].applyTypeStr = getLableFromList(APPLY_TYPE_LIST, this.tableHistoryData[i].taskType);
                this.tableHistoryData[i].taskStatusStr = getLableFromList(APPLY_STATUS_LIST, this.tableHistoryData[i].taskStatus);
                this.tableHistoryData[i].applyUserName = this.tableHistoryData[i].applyUser ? this.tableHistoryData[i].applyUser.name : '';
                this.tableHistoryData[i].applyTimeStr = dateFormat(new Date(this.tableHistoryData[i].applyTime));
                this.tableHistoryData[i].sampleStatusPhaseStr = getLableFromList(this.sampleStatusPhaseList, this.tableHistoryData[i].sampleStatusPhase);
              }
            }

            console.log(this.tableHistoryData);

            // this.currentHistoryPage = res.data.result.pageable.pageNumber;
            this.pageHistorySize = res.data.result.pageable.pageSize;
            this.pageHistoryTotal = res.data.result.totalElements;
          });
      },
      // 表头展示
      getTableHeaderLabel(str) {
        return this.getTableSomeValue(str, 'alias');
      },
      // 获取表格列的宽度
      getTableColumnWidth(str) {
        return this.getTableSomeValue(str, 'width');
      },
      // 获取表格列是否是固定的
      getTableColumnFixed(str) {
        return this.getTableSomeValue(str, 'fixed');
      },
      // 获取表格列是否排序
      getTableColumnSort(str) {
        return this.getTableSomeValue(str, 'sort');
      },
      // 获取表格表头，列宽度，列固定，列排序的值
      getTableSomeValue(str, keyName) {
        for (var value of this.tableHeaderArr) {
          if (value['name'] == str) {
            return value[keyName];
          }
        }
      }
    }
  }
</script>
